# tanchishe_pic.py
import pygame
import time
from constants import *
from snake import Snake
from food import Food


def draw_elements(snake, food, score):
    # 清屏
    screen.fill(BLACK)

    # 绘制蛇 - 使用精灵图
    snake.draw()

    # 绘制食物
    food.draw()

    # 显示分数
    score_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))


def game_over(screen, score):
    # 游戏结束界面
    screen.fill(BLACK)
    game_over_text = font.render("GAME OVER!", True, WHITE)
    score_text = font.render(f"Final Score: {score}", True, WHITE)
    screen.blit(game_over_text, (WIDTH // 2 - 150, HEIGHT // 2 - 50))
    screen.blit(score_text, (WIDTH // 2 - 120, HEIGHT // 2 + 20))
    pygame.display.update()
    time.sleep(3)


def show_start_screen():
    print("显示开始界面，等待任意按键")
    """显示开始界面，等待任意按键"""
    screen.fill(BLACK)
    start_text = font.render("Press Any Key to Start", True, WHITE)
    screen.blit(start_text, (WIDTH // 2 - 180, HEIGHT // 2))
    pygame.display.update()

    waiting = True
    while waiting:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:  # 检测任意按键
                waiting = False


def show_pause_screen(snake):
    """通过闪烁窗口标题提示游戏已暂停"""
    paused = True
    original_caption = pygame.display.get_caption()  # 保存原始标题
    flash_timer = 0
    print(snake.body)  # 打印蛇的身体位置

    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                paused = False  # 按空格继续游戏

        # 闪烁标题
        flash_timer += 1
        if flash_timer % 30 < 15:
            pygame.display.set_caption("🎮 Game Paused")
        else:
            pygame.display.set_caption("🎮 Pygame 贪吃蛇")

        pygame.time.delay(50)  # 控制闪烁频率

    # 恢复原始标题
    pygame.display.set_caption(original_caption[0])


def main():
    global SPEED
    snake = Snake()
    food = Food(snake.body)
    score = 0
    running = True

    # test_display_snake_sprites()  # 添加这行
    test_display_food_sprites()  # 添加这行

    # 添加开始界面
    show_start_screen()  # 等待按键

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:  # 按下空格键进入暂停界面
                    show_pause_screen(snake)  # 调用独立暂停界面函数
                elif event.key == pygame.K_UP:
                    snake.change_direction("UP")
                elif event.key == pygame.K_DOWN:
                    snake.change_direction("DOWN")
                elif event.key == pygame.K_LEFT:
                    snake.change_direction("LEFT")
                elif event.key == pygame.K_RIGHT:
                    snake.change_direction("RIGHT")

        # 移动蛇
        snake.move()

        # 碰撞检测
        if snake.check_collision():
            game_over(screen, score)
            return

        # 吃到食物
        if snake.check_eat_food(food.position):
            score += 10
            snake.grow = True
            food.generate(snake.body)  # 重新生成食物
            SPEED += 2  # 速度提升

        # 绘制元素
        draw_elements(snake, food, score)
        pygame.display.update()
        clock.tick(SPEED)


if __name__ == "__main__":
    main()
